package com.bw.goods.controller;

import java.util.Arrays;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bw.common.utils.MD5Util;
import com.bw.common.vo.ShopSeckillVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import com.bw.goods.entity.SeckillActivityEntity;
import com.bw.goods.service.SeckillActivityService;
import com.bw.common.utils.PageUtils;
import com.bw.common.utils.R;

import javax.servlet.http.HttpServletRequest;


/**
 * @author Hao_YunKai
 * @email 2745578447@qq.com
 * @date 2022-02-22 21:44:15
 */
@RestController
@RequestMapping("goods/seckillactivity")
public class SeckillActivityController {
    @Autowired
    private SeckillActivityService seckillActivityService;

    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 根据商品id查询秒杀商品
     */
    @RequestMapping("/seckillByGoodsId")
    public ShopSeckillVo seckillByGoodsId(@RequestParam(value = "pathId", defaultValue = "0") String pathId) {
        String goodsId = redisTemplate.opsForValue().get(pathId);
        if (goodsId != null) {
            Long goodsId2 = Long.valueOf(goodsId);
            return seckillActivityService.seckillByGoodsId(goodsId2);
        } else {
            return null;
        }
    }

    /**
     * 安全优化之 ---接口地址随机化(隐藏)
     * 1.点击秒杀之后，先访问该接口生成一个pathId，并存入redis 返回前端
     * 2.前端带着这个pathId去访问秒杀接口，如果传入的path和从redis取出的不一致，就认为 非法请求
     */
    @RequestMapping(value = "/getPath/{goodsId}")
    public R getPath(@PathVariable("goodsId") Long goodsId) {
        String uuid = UUID.randomUUID().toString().replace("-","");
        /*随机生成 一个 pathId 返回给前端*/
        String pathId = MD5Util.md5(uuid + "1111");
        assert pathId != null;
        redisTemplate.opsForValue().set(pathId,goodsId.toString(),1,TimeUnit.MINUTES);
        return R.ok().put("pathId",pathId);
    }

    /**
     * 秒杀列表
     */
    @RequestMapping("/voList")
    public R voList(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                @RequestParam(value = "size", defaultValue = "10") Integer size,
                                @RequestParam(value = "goodsName", defaultValue = "") String goodsName) {
        PageUtils pageUtils = seckillActivityService.voList(page, size, goodsName);
        return R.ok().put("page", pageUtils);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = seckillActivityService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        SeckillActivityEntity seckillActivity = seckillActivityService.getById(id);

        return R.ok().put("seckillActivity", seckillActivity);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody SeckillActivityEntity seckillActivity) {
        seckillActivityService.save(seckillActivity);
        redisTemplate.boundHashOps("goodsSeckillStockFlag").put(seckillActivity.getGoodsId().toString(),"1");
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody SeckillActivityEntity seckillActivity) {
        seckillActivityService.updateById(seckillActivity);
        // 设置库存状态
        if (seckillActivity.getStockCount() > 0) {
            redisTemplate.boundHashOps("goodsSeckillStockFlag").put(seckillActivity.getGoodsId().toString(),"1");
        } else {
            redisTemplate.boundHashOps("goodsSeckillStockFlag").put(seckillActivity.getGoodsId().toString(),"0");
        }
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids) {
        seckillActivityService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    // 创建订单前 查看库存
    @RequestMapping("setSeckillStock/{goodsId}/{num}")
    public Integer setSeckillStock(@PathVariable("goodsId") Long goodsId, @PathVariable("num") Integer num) {
        return seckillActivityService.setSeckillStock(goodsId, num);
    }

}
